﻿using System;
using System.Data.SqlClient;
using Eassets.Layered.Domain.Asset;
using System.Data;
using System.Collections.ObjectModel;

namespace Eassets.Data.Asset
{
    public class AssetProcurementDetailsData
    {
        /// <summary>
        /// Insert assetProcurementDetails.
        /// </summary>
        /// <param name="assetProcurementDetails"></param>
        public void AddAssetProcurementDetails(AssetProcurementDetails assetProcurementDetails)
        {
            SqlCommand command = new SqlCommand("SP_AddAssetProcurementDetails"); // Name of the SP
             CommonData commonData = new CommonData();commonData.ExecuteNonQuery(ConstructCommand(command, assetProcurementDetails));
        }

        /// <summary>
        /// Update assetProcurementDetails.
        /// </summary>
        /// <param name="assetProcurementDetails"></param>
        public void ModifyAssetProcurementDetails(AssetProcurementDetails assetProcurementDetails)
        {
            SqlCommand command = new SqlCommand("SP_ModifyAssetProcurementDetails"); // Name of the SP
            command.Parameters.AddWithValue("@AssetProcurementDetailsId", assetProcurementDetails.AssetProcurementDetailsId);
             CommonData commonData = new CommonData();commonData.ExecuteNonQuery(ConstructCommand(command, assetProcurementDetails));
        }


        /// <summary>
        /// Delete assetProcurementDetails.
        /// </summary>
        /// <param name="assetProcurementDetailsId"></param>
        public void DeleteAssetProcurementDetails(long assetProcurementDetailsId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteAssetProcurementDetails"); // Name of the SP
            command.Parameters.AddWithValue("@AssetProcurementDetailsId", assetProcurementDetailsId);
            command.CommandType = CommandType.StoredProcedure;
             CommonData commonData = new CommonData();commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// Get assetProcurementDetails by Id.
        /// </summary>
        /// <param name="assetProcurementDetailsId"></param>
        /// <returns></returns>
        public AssetProcurementDetails GetByIdAssetProcurementDetails(long assetProcurementDetailsId)
        {
            SqlCommand command = new SqlCommand("SP_GetAssetProcurementDetailsById");
            command.Parameters.AddWithValue("@AssetProcurementDetailsId", assetProcurementDetailsId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetProcurementDetails assetProcurementDetails = new AssetProcurementDetails();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["AssetProcurementDetailsId"].ToString())))
                {
                    assetProcurementDetails.AssetProcurementDetailsId = Convert.ToInt64(dataReader["AssetProcurementDetailsId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    assetProcurementDetails.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionLine"].ToString())))
                {
                    assetProcurementDetails.TransactionLine = Convert.ToInt16(dataReader["TransactionLine"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetCode"].ToString())))
                {
                    assetProcurementDetails.AssetCode = Convert.ToString(dataReader["AssetCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetSerialCode"].ToString())))
                {
                    assetProcurementDetails.AssetSerialCode = Convert.ToString(dataReader["AssetSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["SerialQuantity"].ToString())))
                {
                    assetProcurementDetails.SerialQuantity = Convert.ToInt16(dataReader["SerialQuantity"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["UnitPrice"].ToString())))
                {
                    assetProcurementDetails.UnitPrice = Convert.ToDecimal(dataReader["UnitPrice"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TotalPrice"].ToString())))
                {
                    assetProcurementDetails.TotalPrice = Convert.ToDecimal(dataReader["TotalPrice"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["NewLifeTime"].ToString())))
                {
                    assetProcurementDetails.NewLifeTime = Convert.ToInt16(dataReader["NewLifeTime"]);
                }
            }
            dataReader.Close();
            return assetProcurementDetails;
        }

        /// <summary>
        /// GetAll assetProcurementDetails.
        /// </summary>
        /// <returns></returns>
        public Collection<AssetProcurementDetails> GetAllAssetProcurementDetails()
        {
            Collection<AssetProcurementDetails> assetProcurementDetailss = new Collection<AssetProcurementDetails>();
            SqlCommand command = new SqlCommand("SP_GetAllAssetProcurementDetailss");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetProcurementDetails assetProcurementDetails;
            while (dataReader.Read())
            {
                assetProcurementDetails = new AssetProcurementDetails();
                if (!(String.IsNullOrEmpty(dataReader["AssetProcurementDetailsId"].ToString())))
                {
                    assetProcurementDetails.AssetProcurementDetailsId = Convert.ToInt64(dataReader["AssetProcurementDetailsId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    assetProcurementDetails.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionLine"].ToString())))
                {
                    assetProcurementDetails.TransactionLine = Convert.ToInt16(dataReader["TransactionLine"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetCode"].ToString())))
                {
                    assetProcurementDetails.AssetCode = Convert.ToString(dataReader["AssetCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetSerialCode"].ToString())))
                {
                    assetProcurementDetails.AssetSerialCode = Convert.ToString(dataReader["AssetSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["SerialQuantity"].ToString())))
                {
                    assetProcurementDetails.SerialQuantity = Convert.ToInt16(dataReader["SerialQuantity"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["UnitPrice"].ToString())))
                {
                    assetProcurementDetails.UnitPrice = Convert.ToDecimal(dataReader["UnitPrice"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TotalPrice"].ToString())))
                {
                    assetProcurementDetails.TotalPrice = Convert.ToDecimal(dataReader["TotalPrice"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["NewLifeTime"].ToString())))
                {
                    assetProcurementDetails.NewLifeTime = Convert.ToInt16(dataReader["NewLifeTime"]);
                }

                assetProcurementDetailss.Add(assetProcurementDetails);
            }
            dataReader.Close();
            return assetProcurementDetailss;
        }

        public SqlCommand ConstructCommand(SqlCommand command, AssetProcurementDetails assetProcurementDetails)
        {
            command.CommandType = CommandType.StoredProcedure;

            //try
            //{
            //    command.Parameters.AddWithValue("@AssetProcurementDetailsId", assetProcurementDetails.AssetProcurementDetailsId);
            //}
            //catch (Exception)
            //{ }
            try
            {
                command.Parameters.AddWithValue("@TransactionId", assetProcurementDetails.TransactionId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@TransactionLine", assetProcurementDetails.TransactionLine);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetCode", assetProcurementDetails.AssetCode);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetSerialCode", assetProcurementDetails.AssetSerialCode);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@SerialQuantity", assetProcurementDetails.SerialQuantity);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@UnitPrice", assetProcurementDetails.UnitPrice);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@TotalPrice", assetProcurementDetails.TotalPrice);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@NewLifeTime", assetProcurementDetails.NewLifeTime);
            }
            catch (Exception)
            { }
            return command;
        }
    }
}

